home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8205 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: news.pge.com!usenet
  2. From: psk3@pge.com (Phillip Knight)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: A curly one for gurus: possible to create a value array of derived objects?
  5. Date: 15 Feb 1996 19:00:34 GMT
  6. Organization: Pacific Gas and Electric
  7. Distribution: world
  8. Message-ID: <4fvvsi$kk8@news02.comp.pge.com>
  9. References: <4fuho2$d3s@mblisd.macqbl.com.au>
  10. NNTP-Posting-Host: psk3ws04.comp.pge.com
  11. Mime-Version: 1.0
  12. Content-Type: Text/Plain; charset=US-ASCII
  13. X-Newsreader: WinVN 0.99.6
  14.  
  15. In article <4fuho2$d3s@mblisd.macqbl.com.au>, steven@macquarie.com.au says...
  16. >
  17. >
  18. >Ok This is a real curly question:
  19. >
  20. >Is it possible to create (and manage) a "value" array of objects from 
  21. >derived classes?
  22. >
  23. >E.g:
  24. >
  25. >struct A { virtual char* name() { return "A"; } };
  26. >struct B : public A { int    data1; virtual char* name() { return "B"; }  };
  27. >struct C : public A { double data2; virtual char* name() { return "C"; }  };
  28. >
  29. >To be able to provide access to an array of B's and C's without occuring
  30. >the overhead of pointer indirection, I am hoping it is somehow possible to
  31. >have something like:
  32. >
  33. >#include <stdio.h>
  34. >main()
  35. >{
  36. >    A* a = new A[10];
  37. >    B b;
  38. >    C c;
  39. >    a[0] = b;
  40. >    a[1] = c;
  41. >    printf("%s\n", a[0].name());
  42. >    printf("%s\n", a[1].name());
  43. >}
  44. >
  45. >
  46. >The output from this (as you may have already guessed) is 
  47. >A
  48. >A
  49. >
  50. >and not 
  51. >B
  52. >C
  53. >
  54. >The problem here is I guess "data slicing" and the use of the default 
  55. operator=. 
  56. >Is it possible to somehow get over this
  57. >by faking a union? I can't use unions in practice because my derived classes
  58. >have (default) constructors.
  59. >
  60. >The idea for this came from the "Composite" pattern in the Design Patterns
  61. >book by Gamma/Helm/Johnson/Vlissides. The "A" class above being the 
  62. "Composite"
  63. >object, and the "B" and "C" being the "Component" objects. 
  64. >
  65. >Thanks for any solutions/suggestions you can provide.
  66. >Steve
  67. >---
  68. >
  69. >////////////////////////////////////////////////////////////////////////////
  70. >// Steven James Brown                       Disclaimer: Any comments or   //
  71. >// Qantitative Applications                 views made are my own and are //
  72. >// Macquarie Bank Limited                   unrelated to those of my      //
  73. >// steven@macquarie.com.au                  employer.                     //
  74. >//                                                                        //
  75. >////////////////////////////////////////////////////////////////////////////
  76. >
  77. >
  78. >
  79. steven, does  class A have an A(B&) copy constructor?  you created an array
  80. of A, and copying a A derived class will get you the A copy constructor.  this
  81. is of course for data -- forget the virtual functions, because you really only
  82. have a class A.  as a latter post points out, using a lval array is the way
  83. to get to those virtual functions.
  84.  
  85. good luck,
  86. phil
  87.  
  88.